﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Profile;
using TestSystem.DAL.TestSystem;

namespace TestSystem.BLL.TestSystem
{
    public class QuizHistory : BusinessEntity
    {
        #region private fields
        private int _historyID;
        private int _quizID;
        private string _userID;
        private DateTime _dateTimeStart;
        private DateTime _dateTimeComplete;
        private int _scores;
        #endregion

        #region public prop and methods
        public int HistoryID
        {
            get { return _historyID; }
            set { _historyID = value; }
        }

        public int QuizID
        {
            get { return _quizID; }
            set { _quizID = value; }
        }

        public string UserID
        {
            get { return _userID; }
            set { _userID = value; }
        }

        public int Scores
        {
            get { return _scores; }
            set { _scores = value; }
        }

        public DateTime DateTimeStart
        {
            get { return _dateTimeStart; }
            set { _dateTimeStart = value; }
        }

        public DateTime DateTimeComplete
        {
            get { return _dateTimeComplete; }
            set { _dateTimeComplete = value; }
        }

        private Quiz _quizTaken = null;
        public Quiz QuizTaken
        {
            get
            {
                if (_quizTaken == null)
                    _quizTaken = Quiz.GetByQuizID(this.QuizID);
                return _quizTaken;
            }
        }

        private ProfileBase _user = null;
        public ProfileBase User
        {
            get
            {
                if (_user == null)
                    _user = ProfileBase.Create(this.UserID);
                return _user;
            }
        }

        public override void Update()
        {
            QuizHistoryDataAccess.Update(this.HistoryID, this.QuizID, this.UserID, this.DateTimeStart, this.DateTimeComplete, this.Scores);
        }

        protected override void Load(IDataReader rdr)
        {
            HistoryID = (int)rdr["HistoryID"];
            Scores = (int)rdr["Scores"];
            UserID = QuizDataAccess.FromDBValue<string>(rdr["UserID"]);
            QuizID = (int)rdr["QuizID"];
            DateTimeStart = (DateTime)rdr["DateTimeStart"];
            DateTimeComplete = (DateTime)rdr["DateTimeComplete"];
        }
        
        #endregion

        #region static methods
        public static List<QuizHistory> GetByQuizID(int quizID)
        {
            return LoadList<QuizHistory>(QuizHistoryDataAccess.GetQuizHistoryByQuizID(quizID));
        }

        public static List<QuizHistory> GetByUserID(string userID)
        {
            return LoadList<QuizHistory>(QuizHistoryDataAccess.GetQuizHistoryByUserID(userID));
        }

        public static int Add(int quizID, string userID, DateTime dateTimeStart, DateTime dateTimeComplete, int scores)
        {
            int id = QuizHistoryDataAccess.Insert(quizID, userID, dateTimeStart, dateTimeComplete, scores);
            return id;
        }

        public static int Add(SqlTransaction tran, int quizID, string userID, DateTime dateTimeStart, DateTime dateTimeComplete, int scores)
        {
            int id = QuizHistoryDataAccess.Insert(tran, quizID, userID, dateTimeStart, dateTimeComplete, scores);
            return id;
        }

        public static void Remove(int historyID)
        {
            QuizHistoryDataAccess.Delete(historyID);
        }

        public static List<QuizHistory> GetAll()
        {
            return LoadList<QuizHistory>(QuizHistoryDataAccess.GetAll());
        }

        public static QuizHistory GetByID(int historyID)
        {
            return LoadEntity<QuizHistory>(QuizHistoryDataAccess.GetByHistoryID(historyID));
        }
        #endregion
    
        
    }
}
